package com.seek.educenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seek.commonutils.JwtUtils;
import com.seek.commonutils.ResponseResult;
import com.seek.commonutils.ordervo.UcenterMemberInfoOrder;
import com.seek.educenter.entity.UcenterMember;
import com.seek.educenter.entity.vo.RegisterVo;
import com.seek.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wml
 * @since 2022-02-19
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    // 登录（账号密码）
    @GetMapping("loginUsername/{username}/{password}")
    public ResponseResult loginUsername(@PathVariable String username, @PathVariable String password) {
        // 调用 service 方法实现登录
        // 返回 token 值，使用 jwt 生成
        String token = ucenterMemberService.loginUsername(username, password);
        return ResponseResult.ok().data("token", token);
    }

    // 登录（手机号验证码）
    @GetMapping("loginMobile/{mobile}/{code}")
    public ResponseResult loginMobile(@PathVariable String mobile, @PathVariable String code) {
        // 调用 service 方法实现登录
        // 返回 token 值，使用 jwt 生成
        String token = ucenterMemberService.loginMobile(mobile, code);
        return ResponseResult.ok().data("token", token);
    }

    // 注册
    @PostMapping("register")
    public ResponseResult registerUser(@RequestBody RegisterVo registerVo) {
        ResponseResult responseResult = ucenterMemberService.register(registerVo);
        return responseResult;
    }

    // 根据 token 获取用户信息
    @GetMapping("getMemberInfo")
    public ResponseResult getMemberInfo(HttpServletRequest request) {
        // 调用 jwt 工具类的方法，根据 request 对象获取头信息，返回用户 id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        return ResponseResult.ok().data("userInfo", ucenterMember);
    }

    // 根据用户 id 获取用户信息（for order）
    @GetMapping("getUserInfoOrder/{id}")
    public UcenterMemberInfoOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        UcenterMemberInfoOrder ucenterMemberOrder = new UcenterMemberInfoOrder();
        BeanUtils.copyProperties(ucenterMember, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    // 查询某一天注册人数
    @GetMapping("countRegister/{date}")
    public ResponseResult countRegister(@PathVariable String date) {
        Integer count = ucenterMemberService.countRegisterByDate(date);
        return ResponseResult.ok().data("countRegister", count);
    }

    // 根据用户 id 获取用户信息
    @GetMapping("{memberId}")
    public ResponseResult getMemberInfoById(@PathVariable String memberId) {
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        return ResponseResult.ok().data("user", ucenterMember);
    }

}

